package com.zx.framework.core.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

import com.zx.framework.core.exception.FrameworkException;


public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {
	
	private static final Logger LOGGER = Logger.getLogger(ObjectUtils.class);
	
	public static boolean isNull(Object o){
		return o==null;
	}
	
	public static boolean isNotNull(Object o){
		return o!=null;
	}
	
	/**
	 * 根据input获取返回的整型值
	 * @param input
	 * @param defaultValue 默认值
	 * @return
	 */
	public static Integer getIntValue(Object input,Integer defaultValue)
	{
		if(input==null)
		{
			return defaultValue;
		}
		else
		{
			if(input instanceof Integer)
			{
				return (Integer) input;
			}
			else if(input instanceof Long)
			{
				return ((Long)input).intValue();
			}else if(input instanceof Double)
			{
				return ((Double)input).intValue();
			}else if(input instanceof Float)
			{
				return ((Float)input).intValue();
			}else if(input instanceof BigDecimal)
			{
				return ((BigDecimal)input).intValue();
			}
			else if(input instanceof BigInteger)
			{
				return ((BigInteger)input).intValue();
			}
			else if(input instanceof String)
			{
                if(StringUtil.isEmpty((String)input)){
                    return defaultValue;
                }
				try {
					return Integer.parseInt((String)input);
				} catch (NumberFormatException e) {
					String msg = "Integer格式转换失败！";
					LOGGER.error(msg,e);
					throw new FrameworkException(msg);
				}
			}
			else if(input instanceof Date)
			{
				return (int)((Date)input).getTime();
			}
			else
			{
				return defaultValue;
			}
		}
	}
	/**
	 * 根据input获取返回的整型值
	 * @param input
	 * @return
	 */
	public static int getIntValue(Object input)
	{
		return getIntValue(input, 0);
	}
	/**
	 * 根据input获取返回的双精度值
	 * @param input
	 * @param defaultValue 默认值
	 * @return
	 */
	public static double getDoubleValue(Object input,double defaultValue)
	{
		if(input==null)
		{
			return defaultValue;
		}
		else
		{
			if(input instanceof Double)
			{
				return ((Double)input).longValue();
			}
			else if(input instanceof Long)
			{
				return (Long)input;
			}
			else if(input instanceof Integer)
			{
				return new Long((Integer)input);
			}
			else if(input instanceof Float)
			{
				return ((Float)input).doubleValue();
			}
			else if(input instanceof BigDecimal)
			{
				return ((BigDecimal)input).doubleValue();
			}
			else if(input instanceof String)
			{
                if(StringUtil.isEmpty((String)input)){
                    return defaultValue;
                }
				try {
					return Double.parseDouble((String)input);
				} catch (NumberFormatException e) {
					String msg = "Double格式转换失败！";
					LOGGER.error(msg,e);
					throw new FrameworkException(msg);
				}
			}
			else if(input instanceof Date)
			{
				return ((Date)input).getTime();
			}
			else
			{
				return defaultValue;
			}
		}
	}
	/**
	 * 根据input获取返回的双精度值
	 * @param input
	 * @return
	 */
	public static double getDoubleValue(Object input)
	{
		return getDoubleValue(input, 0d);
	}
	/**
	 * 根据input获取返回的长整形值
	 * @param input
	 * @param defaultValue 默认值
	 * @return
	 */
	public static long getLongValue(Object input,long defaultValue)
	{
		if(input==null)
		{
			return defaultValue;
		}
		else
		{
			if(input instanceof Long)
			{
				return (Long)input;
			}
			else if(input instanceof Integer)
			{
				return new Long((Integer)input);
			}
			else if(input instanceof Double)
			{
				return ((Double)input).longValue();
			}else if(input instanceof Float)
			{
				return ((Float)input).longValue();
			}
			else if(input instanceof BigDecimal)
			{
				return ((BigDecimal)input).longValue();
			}
			else if(input instanceof String)
			{
				if(StringUtil.isEmpty((String) input)){
					return defaultValue;
				}
				try {
					return Long.parseLong((String)input);
				} catch (NumberFormatException e) {
					String msg = "Long格式转换失败！";
					LOGGER.error(msg,e);
					throw new FrameworkException(msg);
				}
			}
			else if(input instanceof Date)
			{
				return ((Date)input).getTime();
			}
			else
			{
				return defaultValue;
			}
		}
	}
	/**
	 * 根据input获取返回的长整形值
	 * @param input
	 * @return
	 */
	public static long getLongValue(Object input)
	{
		return getLongValue(input, 0);
	}
	/**
	 * 根据input获取返回的字符串值
	 * @param input
	 * @param defaultValue
	 * @return
	 */
	public static String getStringValue(Object input,String defaultValue)
	{
		if(input==null)
		{
			return defaultValue;
		}
		else
		{
			if(input instanceof String)
			{
				String temp = (String)input;
				if(StringUtil.isEmpty(temp))
				{
					temp = defaultValue;
				}
				return temp;
			}else if(input instanceof Integer)
			{
				return ""+input;
			}
			else if(input instanceof Long)
			{
				return ""+input;
			}
			else if(input instanceof Double)
			{
				return ""+input;
			}
			else if(input instanceof Float)
			{
				return ""+input;
			}
			else if(input instanceof Date)
			{
				return DatetimeUtils.dateToString((Date) input, "yyyy-MM-dd HH:mm:ss");
			}else if(input instanceof BigDecimal)
			{
				return ""+((BigDecimal)input).intValue();
			}
			else
			{
				return defaultValue;
			}
		}
	}
	/**
	 * 根据input获取返回的字符串值
	 * @param input
	 * @return
	 */
	public static String getStringValue(Object input)
	{
		return getStringValue(input, "");
	}
	
	public static Date getDateValue(Object input,Date d){
		if(input==null){
			return d;
		}else if(input instanceof String){
			try {
				return DatetimeUtils.parseDate((String) input, patterns);
			} catch (ParseException e) {
				String msg = "日期格式转换失败！";
				LOGGER.error(msg,e);
				throw new FrameworkException(msg);
			}
		}else if(input instanceof Long){
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis((Long) input);
			return calendar.getTime();
		}else if(input instanceof Integer){
			try {
				return DatetimeUtils.parseDate((String) input, "yyyyMMdd");
			} catch (ParseException e) {
				String msg = "日期格式转换失败！";
				LOGGER.error(msg,e);
				throw new FrameworkException(msg);
			}
		}
		else{
			return  d;
		}
	}
	public static Date getDateValue(Object input){
		return getDateValue(input, new Date());
	}
	
	private static String[] patterns;
	static{
		patterns = new String[4];
		patterns[0] = "yyyy-MM-dd" ;
		patterns[1] =("yyyy-MM-dd HH:mm" );  
		patterns[2] =("yyyy-MM-dd HH:mm:ss" );  
		patterns[3] =("yyyy/MM/dd HH:mm:ss" );
		
	}
}
